﻿@model CustomerReminderModel.ConditionModel.AddCategoryConditionModel
@inject AdminAreaSettings adminAreaSettings
@{
    Layout = "~/Areas/Admin/Views/Shared/_AdminPopupLayout.cshtml";
    //page title
    ViewBag.Title = T("Admin.Customers.CustomerReminder.Condition.Category.AddNew").Text;
}

<form asp-controller="CustomerReminder" asp-action="CategoryAddPopup" method="post"
      asp-route-CustomerReminderId="@Context.Request.Query["CustomerReminderId"]"
      asp-route-btnId="@Context.Request.Query["btnId"]"
      asp-route-formId="@Context.Request.Query["formId"]">

    <input asp-for="CustomerReminderId" type="hidden" />
    <input asp-for="ConditionId" type="hidden" />

    

    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-cubes"></i>
                        @T("Admin.Customers.CustomerActionCondition.Category.AddNew")
                    </div>
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="form-group">
                                <admin-label asp-for="SearchCategoryName" class="col-sm-3 control-label" />
                                <div class="col-md-9 col-sm-9">
                                    <admin-input asp-for="SearchCategoryName" />
                                </div>
                            </div>
                            <div class="form-actions">
                                <div class="row">
                                    <div class="col-md-offset-1 col-md-11 col-sm-11 btn-group btn-group-devided">
                                        <button class="btn btn-success filter-submit" id="search-categories">
                                            <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                        </button>
                                        <input type="submit" name="save" class="btn btn-success " value="@T("Admin.Common.Save")" />
                                    </div>
                                </div>
                            </div>
                            <div id="categories-grid"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @if (ViewBag.RefreshPage == true)
    {
    <script type="text/javascript">
           try {window.opener.document.forms['@(Context.Request.Query["formId"])'].@(Context.Request.Query["btnId"]).click();}
             catch (e){}
            window.close();
    </script>
    }

    <script>
                $(document).ready(function () {
                    $("#categories-grid").kendoGrid({
                        dataSource: {
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("CategoryAddPopupList", "CustomerReminder"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: additionalData
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors"
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(adminAreaSettings.DefaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(adminAreaSettings.GridPageSizes)]
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [{
                            field: "Id",
                            headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                            headerAttributes: { style: "text-align:center" },
                            template: "<input type='checkbox' name='SelectedCategoryIds' value='#=Id#' class='checkboxGroups' />",
                            attributes: { style: "text-align:center" },
                            width: 50
                        }, {
                            field: "Breadcrumb",
                            title: "@T("Admin.Catalog.Categories.Fields.Name")"
                        }, {
                            field: "Published",
                            title: "@T("Admin.Catalog.Categories.Fields.Published")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '# if(Published) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                        }]
                    });
                });
    </script>

    <script type="text/javascript">
                    $(document).ready(function() {
                        $('#search-categories').click(function() {
                            var grid = $('#categories-grid').data('kendoGrid');
                            grid.dataSource.page(1);
                            $('.checkboxGroups').prop('checked', false).change();
                            return false;
                        });

                        $("#@Html.FieldIdFor(model => model.SearchCategoryName)").keydown(function(event) {
                            if (event.keyCode == 13) {
                                $("#search-categories").click();
                                return false;
                            }
                        });

                        //wire up checkboxes
                        $('#mastercheckbox').click(function() {
                            $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
                        });

                        $('#categories-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function(e) {
                            updateMasterCheckbox();
                        });
                    });

                    function additionalData() {
                        var data = {
                            SearchCategoryName: $('#@Html.FieldIdFor(model => model.SearchCategoryName)').val()
                        };
                        addAntiForgeryToken(data);
                        return data;
                    }

                    function updateMasterCheckbox() {
                        var numChkBoxes = $('#categories-grid input[type=checkbox][id!=mastercheckbox]').length;
                        var numChkBoxesChecked = $('#categories-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
                        $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                    }

    </script>
</form>
